package com.company.cloud.user.modules.app.controller;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.HttpContextUtils;
import com.company.cloud.user.constant.UserMsgSendTypeEnum;
import com.company.cloud.user.modules.api.constant.UserRegisterWayEnum;
import com.company.cloud.user.modules.api.request.UserRegisterParam;
import com.company.cloud.user.modules.app.controller.req.UserLogin2RegByMobile2CodeParam;
import com.company.cloud.user.modules.app.service.UserCodeService;
import com.company.cloud.user.modules.app.service.UserCompanyService;
import com.company.cloud.user.modules.app.service.UserLoginService;
import com.company.cloud.user.modules.app.service.UserRegisterService;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.bus.producer.UserProducer;
import com.company.cloud.user.modules.cache.service.UserPermissionCacheService;
import com.company.common.server.config.cloud.ProjectConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * (手机号+验证码)登录并注册
 */
@Log4j2
@RestController
@RequestMapping("/app/user/login")
@Api(tags = "UC_(手机号+验证码)登录并注册")
public class UserLogin2RegisterByMobileCodeController {

    @Autowired
    private UserCompanyService userCompanyService;

    @Autowired
    private UserCodeService userCodeService;

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private UserPermissionCacheService userPermissionCacheService;

    @Autowired
    private ProjectConfig projectConfig;

    @Autowired
    private UserProducer userProducer;

    @Autowired
    private UserRegisterService userRegisterService;

    @PostMapping("/login2RegByMobile2Code")
    @ApiOperation(value = "(手机号+验证码)登录并且注册", response = LoginUserInfo.class)
    public BaseResult login2RegByMobile2Code(@RequestBody UserLogin2RegByMobile2CodeParam params) {
        ValidatorUtils.validate(params);
        boolean isReg = false;
        String companyNo = userCompanyService.getCompany2Check(AuthContextUtils.getShareCode(), AuthContextUtils.getCompanyNo());
        userCodeService.validate(companyNo, UserMsgSendTypeEnum.Login_VCode, params.getMobile(), params.getVcode(), true);
        UserInfo userInfo = userLoginService.getRegUserByMobile(companyNo, params.getMobile());
        if (userInfo == null) { // 注册
            UserRegisterParam registerParam = new UserRegisterParam();
            registerParam.setUserTypeOne(params.getUserTypeOne());
            registerParam.setUserTypeTwo(params.getUserTypeTwo());
            registerParam.setSourceOne(params.getSourceOne());
            registerParam.setSourceTwo(params.getSourceTwo());
            registerParam.setUserTags(params.getUserTags());
            registerParam.setReferralCode(AuthContextUtils.getShareCode());
            registerParam.setHeadImg(params.getHeadImg());
            registerParam.setDisFlg(params.getDisFlg());
            registerParam.setCompanyNo(companyNo);
            registerParam.setMobile(params.getMobile());
            registerParam.setRegIp(HttpContextUtils.getClientIP());
            registerParam.setRegWay(UserRegisterWayEnum.Reg_Mobile.getCode());
            userInfo = userRegisterService.register(registerParam, null);
            isReg = true;
        }
        LoginUserInfo data = userLoginService.login(userInfo);
        userPermissionCacheService.loadPermissionRoleCache(data.getCompanyNo());

        if (isReg) {
            userProducer.userRegister(UserRegisterWayEnum.Reg_Mobile.getCode(), params, params.getDisFlg(), data);
        } else {
            userProducer.userLogin("手机号验证码登录", params, data);
        }
        return BaseResult.ok(data);
    }


}
